package org.skydiveapp;

import java.io.File;

import org.skydiveapplibrary.data.Constants;
import org.skydiveapplibrary.repository.SettingsRepository;
import org.skydiveapplibrary.ui.InterfaceManager;
import org.skydiveapplibrary.R;
import org.skydiveapplibrary.ui.DrawingView;
import org.skydiveapplibrary.ui.ImageManager;
import org.skydiveapplibrary.util.FileManager;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

public class LogEntryDiagramActivity extends SherlockActivity
{
	// data
	private boolean isDualDisplay;
	private ImageManager imageManager;
	private FileManager fileManager;
	private String filename;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		SettingsRepository settingsRepository = new SettingsRepository(this);
		if (settingsRepository.getUseLightTheme())
			super.setTheme(R.style.SkydiveAppTheme_Light_SemiTransparent);
		
		super.onCreate(savedInstanceState);

		setContentView(R.layout.log_entry_diagram_view);

		isDualDisplay = this.findViewById(R.id.phoneLayout) == null;

		imageManager = new ImageManager(this);
		fileManager = new FileManager(this);
		
		// init header
    	InterfaceManager.getInstance().setSubtitleText(getSupportActionBar(), 
				getResources().getString(R.string.log_entry_diagram_header));

		// get drawing view
		DrawingView drawingView = (DrawingView) findViewById(R.id.LogEntryDiagramDrawingView);

		// load data
		if (savedInstanceState != null) {
			filename = savedInstanceState.getString(Constants.EXTRA_DIAGRAM_FILENAME);
        }
		else
		{
			filename = getIntent().getStringExtra(Constants.EXTRA_DIAGRAM_FILENAME);
		}

		if (filename != null)
		{
			Bitmap bitmap = imageManager.decodeToScreenSize(filename);
			drawingView.drawBitmap(bitmap);
		}
	}
	
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState)
	{
		super.onSaveInstanceState(savedInstanceState);

		savedInstanceState.putString(Constants.EXTRA_DIAGRAM_FILENAME, 
				filename);
	}
	
	private void clear()
	{
		DrawingView drawingView = (DrawingView) findViewById(R.id.LogEntryDiagramDrawingView);
		drawingView.clear();
	}

	private void save()
	{
		DrawingView drawingView = (DrawingView) findViewById(R.id.LogEntryDiagramDrawingView);

		if (!drawingView.isBitmapChanged())
		{
			// cancelled if not modified
			setResult(RESULT_CANCELED);
		}
		else
		{
			File file = fileManager.writeByteArrayToStorageDir(drawingView.getImageData());
			
			// set result intent
			Intent intent = new Intent();
			intent.putExtra(Constants.EXTRA_DIAGRAM_FILENAME, file.getName());
			setResult(RESULT_OK, intent);
		}

		// finish
		finish();
	}
	
	private void delete()
	{
		// set result intent
		Intent intent = new Intent();
		intent.putExtra(Constants.EXTRA_DIAGRAM_DELETE, true);
		setResult(RESULT_OK, intent);
		
		// finish
		finish();
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	MenuInflater inflater = getSupportMenuInflater();
    	inflater.inflate(R.menu.menu_open_log_entry, menu);
    	inflater.inflate(R.menu.menu_save, menu);
    	inflater.inflate(R.menu.menu_clear, menu);
    	inflater.inflate(R.menu.menu_delete, menu);
    	inflater.inflate(R.menu.menu_back, menu);
    	return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
    	menu.findItem(R.id.menu_back).setVisible(isDualDisplay);
    	menu.findItem(R.id.menu_open_log_entry)
    		.setVisible(getIntent().getBooleanExtra(Constants.EXTRA_GOTO_LOG_ENTRY, false));
    	return true;
    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		int itemId = item.getItemId();
		
		if (itemId == R.id.menu_open_log_entry)
		{
			// set result intent
			Intent intent = new Intent();
			intent.putExtra(Constants.EXTRA_GOTO_LOG_ENTRY, true);
			setResult(RESULT_OK, intent);
			// close
			close();
			return true;
		}
		else if (itemId == R.id.menu_save)
		{
			save();
			return true;
		}
		else if (itemId == R.id.menu_delete)
		{
			delete();
			return true;
		}
		else if (itemId == R.id.menu_back)
		{
			close();
			return true;
		}
		else if (itemId == R.id.menu_clear)
		{
			clear();
			return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onBackPressed()
	{
		close();
	}

	private void close()
	{
		finish();
	}
}
